package com.toogu.mazhangmen.ui.main;

import android.app.Activity;
import android.widget.Toast;

import com.toogu.mazhangmen.util.EquipmentUtil;

import java.util.ArrayList;

public class MainModel implements MainContract.IModel{
    private static final String TAG = "MainModel";
    private Activity activity = null;
    private Delegate delegate = null;

    private int MODE = EquipmentUtil.Door;
    private String search = null;

    public MainModel(Activity activity){
        this.activity = activity;
    }
    @Override
    public void onCreate() {
        getEquipment(MODE,search);
    }

    @Override
    public void onResume() {

    }

    @Override
    public void onPause() {

    }

    @Override
    public void onDestory() {

    }
    @Override
    public void setModelDelegate(Delegate modelDelegate) {
        delegate=modelDelegate;
    }


    @Override
    public void getEquipment(int MODE,String search) {
        this.MODE = MODE;
        this.search = search;
        EquipmentUtil equipmentUtil = new EquipmentUtil();
        equipmentUtil.getNewDate(activity, new EquipmentUtil.GetCallback() {
            @Override
            public void onFailure(String message) {
                activity.runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        delegate.onHideWaiting();
                        delegate.onSetProjectList(new String[]{"请选择"});
                        delegate.onDataChanged(new ArrayList<>());
                        Toast.makeText(activity,message,Toast.LENGTH_SHORT).show();
                    }
                });
            }
            @Override
            public void onSuccess() {
                activity.runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        delegate.onHideWaiting();
                        if(MODE<0) delegate.onSetProjectList(equipmentUtil.getProjectListName());
                        switch (MODE){
                            case EquipmentUtil.Door:
                                delegate.onDataChanged(equipmentUtil.getAll());
                                break;
                            case EquipmentUtil.Help:
                                delegate.onDataChanged(equipmentUtil.getHelp());
                                break;
                            case EquipmentUtil.OutLine:
                                delegate.onDataChanged(equipmentUtil.getOutLine());
                                break;
                            case EquipmentUtil.Search:
                                delegate.onDataChanged(equipmentUtil.getSearch(search));
                                break;
                            default:
                                delegate.onDataChanged(equipmentUtil.getProject(MODE));
                                break;
                        }
                    }
                });
            }
        });
    }

    @Override
    public void getProject(int position) {

    }
}
